home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / HeapTools.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  5.5 KB  |  206 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        HeapTools.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef    __HEAPTOOLS__
  15. #define    __HEAPTOOLS__
  16.  
  17. #ifndef    __MEMORY__
  18. #include "Memory.h"
  19. #endif
  20.  
  21. #ifndef    __OSUTILS__
  22. #include "OSUtils.h"
  23. #endif
  24.  
  25. #ifndef    __FILES__
  26. #include "Files.h"
  27. #endif
  28.  
  29. class ostream;
  30.  
  31. #pragma push
  32. #pragma segment HeapTools
  33.  
  34. /***********************************|****************************************/
  35.  
  36. struct BlockHeader24
  37. {
  38.     unsigned            fType : 2;
  39.     unsigned            fUnused : 2;
  40.     unsigned            fCorrection : 4;
  41.     unsigned            fPhysicalSize : 24;
  42.  
  43.     union {
  44.         unsigned        fRelativeHandle;
  45.         Zone*            fZone;
  46.     } u;
  47. };
  48.  
  49. /***********************************|****************************************/
  50.  
  51. struct BlockHeader32
  52. {
  53.     unsigned            fType : 2;
  54.     unsigned            fUnused : 6;
  55.     unsigned             fLocked : 1;
  56.     unsigned             fPurgeable : 1;
  57.     unsigned             fResource : 1;
  58.     unsigned             fReserved : 13;
  59.     unsigned            fCorrection : 8;
  60.     unsigned            fPhysicalSize;
  61.  
  62.     union {
  63.         unsigned        fRelativeHandle;
  64.         Zone*            fZone;
  65.     } u;
  66. };
  67.  
  68. /***********************************|****************************************/
  69.  
  70. class BlockInfo
  71. {
  72. public:        BlockInfo ();
  73.             BlockInfo ( const Zone* );
  74.             BlockInfo ( const BlockInfo& );
  75.             ~BlockInfo ();
  76.  
  77.             BlockInfo&        operator = ( const BlockInfo& );
  78.             Boolean            operator == ( register const BlockInfo& ) const;
  79.             Boolean            operator != ( const BlockInfo& ) const;
  80.  
  81.     enum BlockType { kFree, kNonrelocatable, kRelocatable };
  82.  
  83.             void            Set ( const void* blockHeader );
  84.  
  85.             BlockType        GetType () const;
  86.  
  87.             unsigned long    GetLogicalSize () const;
  88.             const void*        GetLogicalStart () const;
  89.  
  90.             unsigned long    GetPhysicalSize () const;
  91.             const void*        GetPhysicalStart () const;
  92.  
  93.             unsigned long    BlockHeaderSize () const;
  94.             const void*        GetIdentifierAddress () const;
  95.  
  96.             void            SetMarked ( Boolean = true );
  97.             Boolean            IsMarked () const;
  98.  
  99.             OSErr            Write ( short RefNum ) const;
  100.             OSErr            Read ( short RefNum );
  101.  
  102.             ostream&        operator >> ( ostream& ) const;
  103.  
  104. private:    ostream&        Stream24 ( ostream& ) const;
  105.             ostream&        Stream32 ( ostream& ) const;
  106.             void*            NextBlockHeader () const;
  107.  
  108.             const Zone*        fZone;
  109.             const void*        fStart;
  110.             Boolean            fMode32;
  111.             Boolean            fMarked;
  112.             
  113.         union {
  114.             BlockHeader24    f24;
  115.             BlockHeader32    f32;
  116.         } fHeader;
  117.  
  118.     friend class THeapWalker;
  119. };
  120.  
  121. /***********************************|****************************************/
  122.  
  123. class THeapWalker
  124. {
  125. public:        THeapWalker ( const Zone* zone );
  126.     virtual    ~THeapWalker ();
  127.  
  128.             void                    Reset ();
  129.             Boolean                    Next ();
  130.             const BlockInfo&        GetCurrent ();
  131.  
  132. private:    THeapWalker ( const THeapWalker& );
  133.             THeapWalker&            operator = ( const THeapWalker& );
  134.  
  135.             const Zone*                fZone;
  136.             const void*                fStart;
  137.             const void*                fEnd;
  138.             const void*                fCurrent;
  139.             BlockInfo                fBlock;
  140. };
  141.  
  142. /***********************************|****************************************/
  143.  
  144. class THeapState
  145. {
  146. public:        THeapState ();
  147.             THeapState ( const Zone* );
  148.             THeapState ( const FSSpec& );
  149.             THeapState ( const THeapState& );
  150.             ~THeapState ();
  151.  
  152.             THeapState&                operator = ( const THeapState& );
  153.             Boolean                    operator == ( const THeapState& ) const;
  154.             Boolean                    operator != ( const THeapState& ) const;
  155.  
  156.             THeapState&                operator -= ( THeapState& );
  157.             THeapState&                operator &= ( THeapState& );
  158.             THeapState&                operator ^= ( THeapState& );
  159.  
  160.             unsigned long            BlockCount () const;
  161.             unsigned long            HeapSize () const;
  162.             const BlockInfo&        operator [] ( unsigned long zeroBasedIndex ) const;
  163.             Boolean                    Contains ( const BlockInfo& ) const;
  164.  
  165.             void                    MarkAllBlocks ( Boolean = true );
  166.             void                    UnmarkAllBlocks ();
  167.             unsigned long             MarkedBlocksCount () const;
  168.             unsigned long            MarkedBlocksSize () const;
  169.  
  170.             OSErr                    Dump ();    // calls StdPutFile
  171.             OSErr                    Dump ( const StringPtr prompt );    // calls StdPutFile
  172.             OSErr                    Dump ( const FSSpec& );
  173.  
  174.             OSErr                    Load ();    // calls StdGetFile
  175.             OSErr                    Load ( const FSSpec& );
  176.  
  177.             ostream&                operator >> ( ostream& ) const;
  178.  
  179. protected:    OSErr                    Copy ( const Zone* );
  180.             OSErr                    Copy ( const THeapState& );
  181.  
  182. private:    unsigned long            fCount;
  183.             BlockInfo*                fBlocks;
  184. };
  185.  
  186. /***********************************|****************************************/
  187.  
  188. inline ostream& operator << ( ostream& s, const BlockInfo& i ) { return i >> s; }
  189. inline ostream& operator << ( ostream& s, const THeapState& i ) { return i >> s; }
  190. inline unsigned long BlockInfo::BlockHeaderSize () const { return fMode32 ? 12 : 8; }
  191. inline unsigned long BlockInfo::GetPhysicalSize () const { return fMode32 ? fHeader.f32.fPhysicalSize : fHeader.f24.fPhysicalSize; }
  192. inline const void* BlockInfo::GetPhysicalStart () const { return fStart; }
  193. inline unsigned long BlockInfo::GetLogicalSize () const { return fMode32 ? fHeader.f32.fPhysicalSize - fHeader.f32.fCorrection - 12 : fHeader.f24.fPhysicalSize - fHeader.f24.fCorrection - 8; }
  194. inline void* BlockInfo::NextBlockHeader () const { return (void*) ( (unsigned long) fStart + GetPhysicalSize () ); }
  195. inline void BlockInfo::SetMarked ( Boolean marked ) { fMarked = marked; }
  196. inline Boolean BlockInfo::IsMarked () const { return fMarked; }
  197. inline unsigned long THeapState::BlockCount () const { return fCount; }
  198. inline Boolean BlockInfo::operator != ( const BlockInfo& that ) const { return !operator == ( that ); }
  199. inline BlockInfo::BlockType BlockInfo::GetType () const { return (BlockType) fHeader.f32.fType; }
  200.  
  201. #pragma pop
  202.  
  203. /***********************************|****************************************/
  204.  
  205. #endif    // __HEAPTOOLS__
  206.